package com.linhz.demo07.Sychronized;

public class RunnableImpl01 implements Runnable{
    private int ticket = 100;
    @Override
    public void run() {
        while (true){
            payTickets();
//            if (ticket>0){
//                try{
//                    Thread.sleep(10);
//                }catch (InterruptedException e){
//                    e.printStackTrace();
//                }
//                System.out.println(Thread.currentThread().getName()+"-->正在卖"+ticket+"张票");
//                ticket--;
//            }
        }
    }

    public synchronized void payTickets(){
        if (ticket>0){
            try{
                Thread.sleep(10);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"-->正在卖"+ticket+"张票");
            ticket--;
        }
    }
}
